package com.ruoyi.project.distribute.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.distribute.domain.HosDocumentLog;
import com.ruoyi.project.distribute.mapper.HosDocumentLogMapper;
import com.ruoyi.project.distribute.service.IHosDocumentLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author anran
 */
@Service
public class HosDocumentLogServiceImpl implements IHosDocumentLogService
{
    @Autowired
    private HosDocumentLogMapper hosDocumentLogMapper;

    /**
     *  创建单据报单信息
     * @param hosDocumentLog 创建信息
     * @return 创建结果
     */
    @Override
    public int insertHosDocumentLog(HosDocumentLog hosDocumentLog)
    {
        String pre = "CG";
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        String nextIdStr = snowflake.nextIdStr();
        String randomID = pre + nextIdStr;
        //生成单据ID
        hosDocumentLog.setDocumentlogId(randomID);
        hosDocumentLog.setCreateTime(DateUtils.getNowDate());
        System.out.println(hosDocumentLog);
        return hosDocumentLogMapper.insertHosDocumentLog(hosDocumentLog);
    }

    /**
     *  查询单据全部新消息
     *
     * @param hosDocumentLog 查询条件
     * @return 查询结果集合
     */
    @Override
    public List<HosDocumentLog> selectHosDocumentList(HosDocumentLog hosDocumentLog)
    {
        return hosDocumentLogMapper.selectHosDocumentList(hosDocumentLog);
    }

    /**
     * 根据单据ID和供应商ID查询 对应信息
     *
     * @param documentlogId 单据ID
     * @param provide  供应商ID
     * @return 查询结果
     */
    @Override
    public
    List<HosDocumentLog> selectHosDocumentLogByDocumentLogIdAndProvideId(String documentlogId, Long provide)
    {
        return hosDocumentLogMapper.selectHosDocumentLogByDocumentLogIdAndProvideId(documentlogId, provide);
    }

    /**
     * 根据供应商ID 和 申请人 查询单据报单表
     *
     * @param provideId 供应商ID
     * @param documentlogApply 申请人
     * @return 查询结果集合
     */
    @Override
    public List<HosDocumentLog> selectHosDocumentLogInAudit(Long provideId, String documentlogApply)
    {
        return hosDocumentLogMapper.selectHosDocumentLogInAudit(provideId, documentlogApply);
    }

    /**
     * 成功
     * 修改单据报单表 审核状态
     *
     * @return
     */
    @Override
    public int updateHosDocumentLogAudit(String[] documentlogId)
    {
        String updateBy = SecurityUtils.getUsername();

        int i = 0;
        for (String s : documentlogId)
        {
            hosDocumentLogMapper.updateHosDocumentLogAudit(s, updateBy);
            i++;
        }
        return i > 0 ? 1 : 0;
    }

    /**
     * 失败
     * 修改单据报单表 审核状态
     *
     * @return
     */
    @Override
    public int updateHosDocumentLogAuditError(String[] documentlogId)
    {
        String updateBy = SecurityUtils.getUsername();

        int i = 0;
        for (String s : documentlogId)
        {
            hosDocumentLogMapper.updateHosDocumentLogAuditError(s, updateBy);
            i++;
        }
        return i > 0 ? 1 : 0;
    }

    /**
     * 提交 已审核
     * 修改单据报单表 审核状态
     *
     * @param documentlogId
     * @return
     */
    @Override
    public int updateHosDocumentLogAudited(String documentlogId)
    {
        String updateBy = SecurityUtils.getUsername();
        return hosDocumentLogMapper.updateHosDocumentLogAudited(documentlogId,updateBy);
    }

    /**
     * 提交 作废
     * 修改单据报单表 审核状态
     *
     * @param documentlogId
     * @return
     */
    @Override
    public int updateHosDocumentLogInvalid(String documentlogId)
    {
        return hosDocumentLogMapper.updateHosDocumentLogInvalid(documentlogId);
    }

    /**
     * 提交 入库成功
     * 修改单据报单表 审核状态
     *
     * @param documentlogId
     * @return
     */
    @Override
    public int updateHosDocumentLogSuccess(String documentlogId)
    {
        String createBy = SecurityUtils.getUsername();
        return hosDocumentLogMapper.updateHosDocumentLogSuccess(documentlogId,createBy);
    }

    /**
     * 提交审核 待审核
     * 修改单据保单表 审核状态
     *
     * @param documentlogId
     * @return
     */
    @Override
    public int updateHosDocumentLogToAudit(String documentlogId)
    {
        return hosDocumentLogMapper.updateHosDocumentLogToAudit(documentlogId);
    }

}
